package com.springboot.sharding;

import com.google.common.collect.Range;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.hint.HintShardingValue;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
import org.apache.tomcat.util.buf.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

/**
 * @Author: TongRui乀
 * @Date: 2019-11-20 15:12
 * @description：
 */
@Slf4j
public class DbShardingAlgorithm implements HintShardingAlgorithm<Integer> {

    public DbShardingAlgorithm() {
    }

    /*@Override
    public Collection<String> doSharding(Collection<String> collection, RangeShardingValue<Integer> rangeShardingValue) {

        log.info("表分片策略[{}]", StringUtils.join(collection,','));

        Range<Integer> valueRange = rangeShardingValue.getValueRange();

        Integer low = valueRange.lowerEndpoint();

        Integer end = valueRange.upperEndpoint();

        log.info("数据库区间分片策略 下限：[{}] 上线[{}]", low, end);

        return Arrays.asList("db1");
    }
*/

    @Override
    public Collection<String> doSharding(Collection<String> collection, HintShardingValue<Integer> hintShardingValue) {

        String db = "db";

        ArrayList<String> list = new ArrayList<>();

        hintShardingValue.getValues().forEach(value->{
            list.add(db + value);
        });

        return list;
    }
}
